package com.me.EpicVN;


import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;


import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.me.EpicVN.Data.NewsDetails;
import com.me.EpicVN.Utli.GlobalVariable;
import com.me.EpicVN.Utli.htmlReader;

public class EpicVNActivity extends Activity {
    /** Called when the activity is first created. */
	
	static Context mContext;
	static String check = "";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext 		= this;
        if(GlobalVariable.isOnline(mContext)==true)
        {
        	setContentView(R.layout.main);
     		new syncTask().execute();
        }
        else
        {
        	//Toast.makeText(mContext, "Can't connect internet", Toast.LENGTH_SHORT).show();
        	setContentView(R.layout.connect);
        	TextView t = (TextView)findViewById(R.id.txtConn);
        	t.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
				System.exit(1);	
				}
			});
        }
       
        
    }
    
    public static class syncTask extends AsyncTask<String, Integer, Integer>
	{
		private ProgressDialog mProgressDialog;
		List<NewsDetails> l = new ArrayList<NewsDetails>();
		
		@Override
		protected void onPreExecute()
		{
			mProgressDialog = new ProgressDialog(mContext);
			mProgressDialog.setMessage("Loading...");
			mProgressDialog.show();
		}
		
		

		@Override
		protected Integer doInBackground(String... params)
		{
			htmlReader hh;
			try {
					Log.w("hung", "vo day a moio ");
					hh = new htmlReader(new URL("http://epic.vn/moi"));
					hh.getLinksByClass("col-1");
				
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			for(int i = 0; i<GlobalVariable.listImage.size(); i++)
			{
				Log.d("ACTiVIty", "listImage = "+GlobalVariable.listImage.get(i));
				l.add(new NewsDetails(GlobalVariable.listImage.get(i), GlobalVariable.listTitle.get(i), null, null));
			}
            GlobalVariable.listNews = l;
			return 0;
		}		
				
		@Override
		protected void onPostExecute(Integer result)
		{
			
			mProgressDialog.cancel();
			
			Intent myIntent = new Intent(mContext, ShowActivity.class);
			mContext.startActivity(myIntent);
			((EpicVNActivity)mContext).finish();
		}
	}
    
}